<!DOCTYPE html><html lang="zh-CN" data-theme="dark"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width,initial-scale=1"><title>手写一个mini版本vue-router | 北城韩雨</title><meta name="keywords" content="Vue 源码"><meta name="author" content="北城韩雨"><meta name="copyright" content="北城韩雨"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#0d0d0d"><meta name="description" content="手写一个lmini版本vue-router">
<meta property="og:type" content="article">
<meta property="og:title" content="手写一个mini版本vue-router">
<meta property="og:url" content="http://example.com/2019/03/18/%E6%89%8B%E5%86%99%E4%B8%80%E4%B8%AAmini%E7%89%88vue-router/index.html">
<meta property="og:site_name" content="北城韩雨">
<meta property="og:description" content="手写一个lmini版本vue-router">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic.vjshi.com%2F2018-12-05%2F66b696ea615f3567a32b65f93611a814%2F00004.jpg%3Fx-oss-process%3Dstyle%2Fwatermark&refer=http%3A%2F%2Fpic.vjshi.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1629530687&t=c2e3c97de6dbdc29e77593d5f4dc7fb5">
<meta property="article:published_time" content="2019-03-17T16:00:00.000Z">
<meta property="article:modified_time" content="2019-06-02T16:00:00.000Z">
<meta property="article:author" content="北城韩雨">
<meta property="article:tag" content="Vue-Router">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic.vjshi.com%2F2018-12-05%2F66b696ea615f3567a32b65f93611a814%2F00004.jpg%3Fx-oss-process%3Dstyle%2Fwatermark&refer=http%3A%2F%2Fpic.vjshi.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1629530687&t=c2e3c97de6dbdc29e77593d5f4dc7fb5"><link rel="shortcut icon" href="/img/favicon.png"><link rel="canonical" href="http://example.com/2019/03/18/%E6%89%8B%E5%86%99%E4%B8%80%E4%B8%AAmini%E7%89%88vue-router/"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = { 
  root: '/',
  algolia: undefined,
  localSearch: undefined,
  translate: undefined,
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '',
  date_suffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: undefined,
  lightbox: 'mediumZoom',
  Snackbar: undefined,
  source: {
    jQuery: 'https://cdn.jsdelivr.net/npm/jquery@latest/dist/jquery.min.js',
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/justifiedGallery/dist/js/jquery.justifiedGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/justifiedGallery/dist/css/justifiedGallery.min.css'
    },
    fancybox: {
      js: 'https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.js',
      css: 'https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.css'
    }
  },
  isPhotoFigcaption: false,
  islazyload: false,
  isanchor: false
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: '手写一个mini版本vue-router',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2019-06-03 00:00:00'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
    const detectApple = () => {
      if (GLOBAL_CONFIG_SITE.isHome && /iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
        document.documentElement.classList.add('apple')
      }
    }
    detectApple()
    })(window)</script><link rel="stylesheet" href="/css/custom.css"  media="defer" onload="this.media='all'"><link rel="stylesheet" href="/css/coin.css"  media="defer" onload="this.media='all'"><link rel="stylesheet" href="/css/liuxing.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/l-lin/font-awesome-animation/dist/font-awesome-animation.min.css"  media="defer" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/swiper/swiper-bundle.min.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/zhheo/JS-Heo@main/bb/showbb_in_index.css"><!-- hexo injector head_end start --><link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/4.1.1/animate.min.css" media="print" onload="this.media='screen'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/l-lin/font-awesome-animation/dist/font-awesome-animation.min.css" media="defer" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/hexo-butterfly-tag-plugins-plus/lib/tag_plugins.min.css" media="defer" onload="this.media='all'"><!-- hexo injector head_end end --><meta name="generator" content="Hexo 5.3.0"></head><body><div id="web_bg"></div><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src="/img/my.jpg" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="site-data"><div class="data-item is-center"><div class="data-item-link"><a href="/archives/"><div class="headline">文章</div><div class="length-num">65</div></a></div></div><div class="data-item is-center"><div class="data-item-link"><a href="/tags/"><div class="headline">标签</div><div class="length-num">23</div></a></div></div><div class="data-item is-center"><div class="data-item-link"><a href="/categories/"><div class="headline">分类</div><div class="length-num">21</div></a></div></div></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/"><i class="fa-fw fas fa-home faa-tada"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/shuoshuo/"><i class="fa-fw fas fa-archive faa-tada"></i><span> 叨逼叨</span></a></div><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/tags/"><i class="fa-fw fas fa-tags faa-tada"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/categories/"><i class="fa-fw fas fa-folder-open faa-tada"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/link/"><i class="fa-fw fas fa-link faa-tada"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/comments/"><svg class="icon faa-tada" aria-hidden="true"><use xlink:href="#icon-message"></use></svg><span> 留言板</span></a></div><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/about/"><i class="fa-fw fas fa-heart faa-tada"></i><span> 个人简介</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic.vjshi.com%2F2018-12-05%2F66b696ea615f3567a32b65f93611a814%2F00004.jpg%3Fx-oss-process%3Dstyle%2Fwatermark&amp;refer=http%3A%2F%2Fpic.vjshi.com&amp;app=2002&amp;size=f9999,10000&amp;q=a80&amp;n=0&amp;g=0n&amp;fmt=jpeg?sec=1629530687&amp;t=c2e3c97de6dbdc29e77593d5f4dc7fb5')"><nav id="nav"><span id="blog_name"><a id="site-name" href="/">北城韩雨</a></span><div id="menus"><div class="menus_items"><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/"><i class="fa-fw fas fa-home faa-tada"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/shuoshuo/"><i class="fa-fw fas fa-archive faa-tada"></i><span> 叨逼叨</span></a></div><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/tags/"><i class="fa-fw fas fa-tags faa-tada"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/categories/"><i class="fa-fw fas fa-folder-open faa-tada"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/link/"><i class="fa-fw fas fa-link faa-tada"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/comments/"><svg class="icon faa-tada" aria-hidden="true"><use xlink:href="#icon-message"></use></svg><span> 留言板</span></a></div><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/about/"><i class="fa-fw fas fa-heart faa-tada"></i><span> 个人简介</span></a></div></div><div id="toggle-menu"><a class="site-page"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">手写一个mini版本vue-router</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2019-03-17T16:00:00.000Z" title="发表于 2019-03-18 00:00:00">2019-03-18</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2019-06-02T16:00:00.000Z" title="更新于 2019-06-03 00:00:00">2019-06-03</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/%E6%BA%90%E7%A0%81/">源码</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-wordcount"><i class="far fa-file-word fa-fw post-meta-icon"></i><span class="post-meta-label">字数总计:</span><span class="word-count">4.2k</span><span class="post-meta-separator">|</span><i class="far fa-clock fa-fw post-meta-icon"></i><span class="post-meta-label">阅读时长:</span><span>16分钟</span></span><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="手写一个mini版本vue-router"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><h1 id="mini版本vue-router"><a href="#mini版本vue-router" class="headerlink" title="mini版本vue-router"></a>mini版本vue-router</h1><h2 id="router的挂载流程"><a href="#router的挂载流程" class="headerlink" title="router的挂载流程"></a>router的挂载流程</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//首先我们看看原本的vue-router都挂在流程 </span></span><br><span class="line">router.js文件</span><br><span class="line"><span class="comment">//引入Vue</span></span><br><span class="line"><span class="keyword">import</span> Vue <span class="keyword">from</span> <span class="string">&#x27;vue&#x27;</span></span><br><span class="line"><span class="comment">//引入了vue-router 这里也就是我们构造函数</span></span><br><span class="line"><span class="keyword">import</span> Router <span class="keyword">from</span> <span class="string">&#x27;vue-router&#x27;</span></span><br><span class="line"><span class="comment">//引入了对应的组件</span></span><br><span class="line"><span class="keyword">import</span> Home <span class="keyword">from</span> <span class="string">&#x27;./views/Home.vue&#x27;</span></span><br><span class="line"><span class="keyword">import</span> About <span class="keyword">from</span> <span class="string">&#x27;./views/About.vue&#x27;</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 使用了vue的use方法 把router挂在到了 vue上</span></span><br><span class="line">Vue.use(Router);</span><br><span class="line"><span class="comment">//配置了模式 和 基础地址 还有里有表 把这些当作了配置项目传进了我们的router构造函数 导出了我们router构造函数 挂在到了vue构造函数上</span></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> <span class="keyword">new</span> Router(&#123;</span><br><span class="line">  mode: <span class="string">&#x27;hash&#x27;</span>,</span><br><span class="line">  base: process.env.BASE_URL,</span><br><span class="line">  routes: [ </span><br><span class="line">    &#123;</span><br><span class="line">      path: <span class="string">&#x27;/&#x27;</span>,</span><br><span class="line">      name: <span class="string">&#x27;home&#x27;</span>,</span><br><span class="line">      component: Home</span><br><span class="line">    &#125;,</span><br><span class="line">    &#123;</span><br><span class="line">      path: <span class="string">&#x27;/about&#x27;</span>,</span><br><span class="line">      name: <span class="string">&#x27;about&#x27;</span>,</span><br><span class="line">      component: About</span><br><span class="line">    &#125;,</span><br><span class="line">  ]</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//main js文件 </span></span><br><span class="line"><span class="keyword">import</span> Vue <span class="keyword">from</span> <span class="string">&#x27;vue&#x27;</span></span><br><span class="line"><span class="keyword">import</span> App <span class="keyword">from</span> <span class="string">&#x27;./App.vue&#x27;</span></span><br><span class="line"><span class="keyword">import</span> router <span class="keyword">from</span> <span class="string">&#x27;./router&#x27;</span></span><br><span class="line"></span><br><span class="line">Vue.config.productionTip = <span class="literal">false</span></span><br><span class="line"><span class="comment">//把我们导出的router的构造函数挂在到了vue的构造函数上</span></span><br><span class="line"><span class="keyword">new</span> Vue(&#123;</span><br><span class="line">  router,</span><br><span class="line">  render: <span class="function"><span class="params">h</span> =&gt;</span> h(App)</span><br><span class="line">&#125;).$mount(<span class="string">&#x27;#app&#x27;</span>)</span><br></pre></td></tr></table></figure>
<h2 id="Vue-use都干了什么"><a href="#Vue-use都干了什么" class="headerlink" title="Vue.use都干了什么"></a>Vue.use都干了什么</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>首先我们Vue.use()  默认会调install方法 所以我们需要在你传进去的构造函数上 添加一个install方法</span><br><span class="line"><span class="number">2.</span>这个install方法的形参 就是你的Vue实例</span><br><span class="line"><span class="number">3.</span>这里我们会把传进来的vue存放一下 方便我们扩展属性 或者 组件（elementtUI）和指令</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//如下所示</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Router</span> </span>&#123;</span><br><span class="line">  <span class="title">constructor</span> (<span class="params"></span>) &#123;</span><br><span class="line">    <span class="built_in">this</span>.a = <span class="string">&#x27;11&#x27;</span> <span class="comment">//测试</span></span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// 默认我们的Vue.use会调用函数install</span></span><br><span class="line"><span class="comment">// 所以我们需要在我们传进去的Router里写个install方法</span></span><br><span class="line"><span class="comment">// 这里的形参就是我们的Vue实例</span></span><br><span class="line">Router.install = <span class="function">(<span class="params">_Vue</span>) =&gt;</span> &#123;</span><br><span class="line">  <span class="keyword">const</span> Vue = _Vue</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> Router</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//我们在new Vue的时候传进去了配置项</span></span><br><span class="line"><span class="keyword">new</span> Vue(&#123;</span><br><span class="line">  router, <span class="comment">//这里把我们的router的构造函数也传进去了</span></span><br><span class="line">  render: <span class="function"><span class="params">h</span> =&gt;</span> h(App)</span><br><span class="line">&#125;).$mount(<span class="string">&#x27;#app&#x27;</span>)</span><br><span class="line"><span class="comment">//然后又通过Vue.use(Router)触发了install函数 拿到了Vue的实例</span></span><br><span class="line">Router.install = <span class="function">(<span class="params">_Vue</span>) =&gt;</span> &#123;</span><br><span class="line"> <span class="comment">//所以当前的Vue实力上是有router的   </span></span><br><span class="line">  <span class="keyword">const</span> Vue = _Vue</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> Router</span><br></pre></td></tr></table></figure>
<h2 id="如何在每个组件上拿到router实例"><a href="#如何在每个组件上拿到router实例" class="headerlink" title="如何在每个组件上拿到router实例"></a>如何在每个组件上拿到router实例</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>我们知道每个Vue组件都是一个Vue的实例 </span><br><span class="line"><span class="number">2.</span>我们在根组件的时候 也据是<span class="keyword">new</span> Vue的时候 会把router传进去</span><br><span class="line"><span class="number">3.</span>那么我们怎么在每个组件都拿到router呢</span><br><span class="line"><span class="comment">//可能我们可以把router 挂在到Vue的原型上 如Vue.prototype.router = xxx </span></span><br><span class="line"><span class="comment">//但是这样做了以后 我们重新 new Vue的时候 也会在原型上增加</span></span><br><span class="line"><span class="number">4.</span>所以这时候为了避免这样 我们就要给每个组件都单独添加一个router 就要使用Vue的api minxin() 混合</span><br></pre></td></tr></table></figure>
<h2 id="Vue-mixin-API"><a href="#Vue-mixin-API" class="headerlink" title="Vue.mixin() API"></a>Vue.mixin() API</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>这个api会把我的<span class="keyword">new</span> Vue的时候 传入的配置对象 给每个组件的属性混合一起</span><br><span class="line"><span class="number">2.</span>内部有个方法 叫beforeCreate() 会把我们组件的声明周期都在一个数组里 然后把他自己放在数组的最前面 所以在组件调用的时候会在组件自己的声明周期之前执行eforeCreate()方法</span><br></pre></td></tr></table></figure>
<h2 id="给每个组件都添加router实例"><a href="#给每个组件都添加router实例" class="headerlink" title="给每个组件都添加router实例"></a>给每个组件都添加router实例</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>我们知道 我们的router实例 实在根组件的时候在会传进去的 所以我们要判断 是不是根组件 如果不是 我们就继续向上找 </span><br><span class="line"><span class="number">2.</span>由于我们的每个组件里都会有beforeCreate() 所以会一直向上找 知道找到根组件</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> Vue</span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Router</span> </span>&#123;</span><br><span class="line">  <span class="title">constructor</span> (<span class="params"></span>) &#123;</span><br><span class="line">    <span class="comment">//这里可以在组件里拿到a的值  </span></span><br><span class="line">    <span class="built_in">this</span>.a = <span class="string">&#x27;11&#x27;</span></span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// 默认我们的Vue.use会调用函数install</span></span><br><span class="line"><span class="comment">// 所以我们需要在我们传进去的Router里写个install方法</span></span><br><span class="line"><span class="comment">// 这里的形参就是我们的Vue实例</span></span><br><span class="line"></span><br><span class="line">Router.install = <span class="function">(<span class="params">_Vue</span>) =&gt;</span> &#123;</span><br><span class="line">  Vue = _Vue</span><br><span class="line">  <span class="comment">// 为了在我们每个Vue的组件中拿到router实例 所以使用mixin方法 而不是使用原型上的那种添加方法</span></span><br><span class="line">  Vue.mixin(&#123;</span><br><span class="line">    <span class="comment">// 在每个组件的生命周期 之前调用 每个组件都会调用所以会一直执行类似递归</span></span><br><span class="line">    beforeCreate () &#123;</span><br><span class="line">      <span class="comment">// 判断你是不是最开始的根组件 也就是我们new Vue的时候传进去的配置</span></span><br><span class="line">      <span class="keyword">if</span> (<span class="built_in">this</span>.$options &amp;&amp; <span class="built_in">this</span>.$options.router) &#123;</span><br><span class="line">        <span class="comment">// 如果是我就赋值</span></span><br><span class="line">        <span class="built_in">this</span>._router = <span class="built_in">this</span>.$options.router</span><br><span class="line">      &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">        <span class="comment">// 如果不是我就继续向上找 这里你也可能没有父亲 可能是新new Vue 所以多了一层判断</span></span><br><span class="line">        <span class="built_in">this</span>._router = <span class="built_in">this</span>.$parent &amp;&amp; <span class="built_in">this</span>.$parent.router</span><br><span class="line">      &#125;</span><br><span class="line">      <span class="comment">// 以上就是让所有的组件都有了router的实例 也可拿到我们传进router构造函数的值</span></span><br><span class="line">      <span class="built_in">console</span>.log(<span class="built_in">this</span>.router.a, <span class="string">&#x27;----- 11 &#x27;</span>)</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;)</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> Router</span><br></pre></td></tr></table></figure>
<h2 id="添加属性"><a href="#添加属性" class="headerlink" title="添加属性"></a>添加属性</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>我们知道在每个Vue的组件里 都可以使用<span class="built_in">this</span>.$route 和 <span class="built_in">this</span>.$route</span><br><span class="line"><span class="number">2.</span>这个其实就是相当于给对象添加了一个属性 这里我们要做数据劫持</span><br><span class="line"><span class="keyword">let</span> Vue</span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Router</span> </span>&#123;</span><br><span class="line">  <span class="title">constructor</span> (<span class="params"></span>) &#123;</span><br><span class="line">    <span class="built_in">this</span>.a = <span class="string">&#x27;11&#x27;</span></span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// 默认我们的Vue.use会调用函数install</span></span><br><span class="line"><span class="comment">// 所以我们需要在我们传进去的Router里写个install方法</span></span><br><span class="line"><span class="comment">// 这里的形参就是我们的Vue实例</span></span><br><span class="line"></span><br><span class="line">Router.install = <span class="function">(<span class="params">_Vue</span>) =&gt;</span> &#123;</span><br><span class="line">  Vue = _Vue</span><br><span class="line">  <span class="comment">// 为了在我们每个Vue的组件中拿到router实例 所以使用mixin方法 而不是使用原型上的那种添加方法</span></span><br><span class="line">  Vue.mixin(&#123;</span><br><span class="line">    <span class="comment">// 在每个组件的生命周期 之前调用 每个组件都会调用所以会一直执行类似递归</span></span><br><span class="line">    beforeCreate () &#123;</span><br><span class="line">      <span class="comment">// 判断你是不是最开始的根组件 也就是我们new Vue的时候传进去的配置</span></span><br><span class="line">      <span class="keyword">if</span> (<span class="built_in">this</span>.$options &amp;&amp; <span class="built_in">this</span>.$options.router) &#123;</span><br><span class="line">        <span class="comment">// 如果是我就赋值</span></span><br><span class="line">        <span class="built_in">this</span>._router = <span class="built_in">this</span>.$options.router</span><br><span class="line">      &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">        <span class="comment">// 如果不是我就继续向上找 这里你也可能没有父亲 可能是新new Vue 所以多了一层判断</span></span><br><span class="line">        <span class="built_in">this</span>._router = <span class="built_in">this</span>.$parent &amp;&amp; <span class="built_in">this</span>.$parent.router</span><br><span class="line">      &#125;</span><br><span class="line">      <span class="comment">// 以上就是让所有的组件都有了router的实例 也可拿到我们传进router构造函数的值</span></span><br><span class="line">      <span class="comment">// 为每个组件添加$router 和 $route 属性 这里的this就是当前的Vue实例也就是组件</span></span><br><span class="line">      <span class="built_in">Object</span>.defineProperty(<span class="built_in">this</span>, <span class="string">&#x27;$router&#x27;</span>, &#123;</span><br><span class="line">        value: &#123;&#125;</span><br><span class="line">      &#125;)</span><br><span class="line">      <span class="built_in">Object</span>.defineProperty(<span class="built_in">this</span>, <span class="string">&#x27;$route&#x27;</span>, &#123;</span><br><span class="line">        value: &#123;&#125;</span><br><span class="line">      &#125;)</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;)</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> Router</span><br></pre></td></tr></table></figure>
<h2 id="生成组件"><a href="#生成组件" class="headerlink" title="生成组件"></a>生成组件</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>以上我们已经完成了增加属性</span><br><span class="line"><span class="number">2.</span>我们都知道 router会有两个组件 一个是router-link 其实就是一个a标签 另一个是router-view 一个渲染的视口</span><br><span class="line"><span class="number">3.</span>所以接下来我们需要创建这个两个组件</span><br></pre></td></tr></table></figure>
<figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> Vue</span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Router</span> </span>&#123;</span><br><span class="line">  <span class="title">constructor</span> (<span class="params"></span>) &#123;</span><br><span class="line">    <span class="built_in">this</span>.a = <span class="string">&#x27;11&#x27;</span></span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// 默认我们的Vue.use会调用函数install</span></span><br><span class="line"><span class="comment">// 所以我们需要在我们传进去的Router里写个install方法</span></span><br><span class="line"><span class="comment">// 这里的形参就是我们的Vue实例</span></span><br><span class="line"></span><br><span class="line">Router.install = <span class="function">(<span class="params">_Vue</span>) =&gt;</span> &#123;</span><br><span class="line">  Vue = _Vue</span><br><span class="line">  <span class="comment">// 为了在我们每个Vue的组件中拿到router实例 所以使用mixin方法 而不是使用原型上的那种添加方法</span></span><br><span class="line">  Vue.mixin(&#123;</span><br><span class="line">    <span class="comment">// 在每个组件的生命周期 之前调用 每个组件都会调用所以会一直执行类似递归</span></span><br><span class="line">    beforeCreate () &#123;</span><br><span class="line">      <span class="comment">// 判断你是不是最开始的根组件 也就是我们new Vue的时候传进去的配置</span></span><br><span class="line">      <span class="keyword">if</span> (<span class="built_in">this</span>.$options &amp;&amp; <span class="built_in">this</span>.$options.router) &#123;</span><br><span class="line">        <span class="comment">// 如果是我就赋值</span></span><br><span class="line">        <span class="built_in">this</span>._router = <span class="built_in">this</span>.$options.router</span><br><span class="line">      &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">        <span class="comment">// 如果不是我就继续向上找 这里你也可能没有父亲 可能是新new Vue 所以多了一层判断</span></span><br><span class="line">        <span class="built_in">this</span>._router = <span class="built_in">this</span>.$parent &amp;&amp; <span class="built_in">this</span>.$parent.router</span><br><span class="line">      &#125;</span><br><span class="line">      <span class="comment">// 以上就是让所有的组件都有了router的实例 也可拿到我们传进router构造函数的值</span></span><br><span class="line">      <span class="comment">// 为每个组件添加$router 和 $route 属性 这里的this就是当前的Vue实例也就是组件</span></span><br><span class="line">      <span class="built_in">Object</span>.defineProperty(<span class="built_in">this</span>, <span class="string">&#x27;$router&#x27;</span>, &#123;</span><br><span class="line">        value: &#123;&#125;</span><br><span class="line">      &#125;)</span><br><span class="line">      <span class="built_in">Object</span>.defineProperty(<span class="built_in">this</span>, <span class="string">&#x27;$route&#x27;</span>, &#123;</span><br><span class="line">        value: &#123;&#125;</span><br><span class="line">      &#125;)</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;)</span><br><span class="line">  <span class="comment">// 生成一个router-link组件 其实就是一个a标签 这里使用了jsx语法 能拿到你标签上传过来的to的值 这里当我们点击的时候a标签会让浏览器地址跳到你传给的地址栏上的值</span></span><br><span class="line">  Vue.component(<span class="string">&#x27;router-link&#x27;</span>, &#123;</span><br><span class="line">    props: &#123;</span><br><span class="line">      to: <span class="built_in">String</span></span><br><span class="line">    &#125;,</span><br><span class="line">    render () &#123;</span><br><span class="line">      <span class="keyword">return</span> <span class="xml"><span class="tag">&lt;<span class="name">a</span> <span class="attr">href</span>=<span class="string">&#123;</span>`$&#123;<span class="attr">this.to</span>&#125;`&#125;&gt;</span>&#123;this.$slots.default&#125;<span class="tag">&lt;/<span class="name">a</span>&gt;</span></span></span><br><span class="line">    &#125;</span><br><span class="line">  &#125;)</span><br><span class="line">  <span class="comment">// 生成视口组件</span></span><br><span class="line">  Vue.component(<span class="string">&#x27;router-view&#x27;</span>, &#123;</span><br><span class="line">    props: &#123;</span><br><span class="line">      to: <span class="built_in">String</span></span><br><span class="line">    &#125;,</span><br><span class="line">    render () &#123;</span><br><span class="line">      <span class="keyword">return</span> <span class="literal">null</span></span><br><span class="line">    &#125;</span><br><span class="line">  &#125;)</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> Router</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//下面我们就在app.vue里使用了这个组件</span></span><br><span class="line">&lt;template&gt;</span><br><span class="line">  &lt;div id=<span class="string">&quot;app&quot;</span>&gt;</span><br><span class="line">    <span class="comment">//这里就是我们刚才使用的组件  </span></span><br><span class="line">    &lt;router-link to=<span class="string">&quot;/index&quot;</span>&gt;去首页&lt;/router-link&gt;</span><br><span class="line">    &lt;router-link to=<span class="string">&quot;/about&quot;</span>&gt;去about页&lt;/router-link&gt;</span><br><span class="line">  &lt;/div&gt;</span><br><span class="line">&lt;/template&gt;</span><br><span class="line"></span><br><span class="line">&lt;script&gt;</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> &#123;</span><br><span class="line">  name: <span class="string">&#x27;App&#x27;</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p><img src="1.gif" alt=""></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//我们看到上面已经实现了组件的跳转 并且地址栏也改变了 接下来就是要拿到我们的路由表 然后 把对应的组件渲染在视口里面</span></span><br></pre></td></tr></table></figure>
<h2 id="拿到路由表"><a href="#拿到路由表" class="headerlink" title="拿到路由表"></a>拿到路由表</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>我们在<span class="keyword">new</span> Router时候 传进去了配置项目 里面就有我们的路由表</span><br><span class="line"><span class="number">2.</span>拿到路由表之后 我们需要吧数据从新渲染一下 变成&#123;<span class="string">&#x27;路径&#x27;</span>:<span class="string">&#x27;对应的组件&#x27;</span>&#125;</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> <span class="keyword">new</span> Router(&#123;</span><br><span class="line">  base: process.env.BASE_URL,</span><br><span class="line">  routes: [</span><br><span class="line">    &#123;</span><br><span class="line">      path: <span class="string">&#x27;/&#x27;</span>,</span><br><span class="line">      name: <span class="string">&#x27;home&#x27;</span>,</span><br><span class="line">      component: Home</span><br><span class="line">    &#125;,</span><br><span class="line">    &#123;</span><br><span class="line">      path: <span class="string">&#x27;/about&#x27;</span>,</span><br><span class="line">      name: <span class="string">&#x27;about&#x27;</span>,</span><br><span class="line">      component: About</span><br><span class="line">    &#125;</span><br><span class="line">  ]</span><br><span class="line">&#125;)</span><br><span class="line"><span class="comment">//变成这样</span></span><br><span class="line">&#123;<span class="string">&#x27;/&#x27;</span>:Home,<span class="string">&#x27;/about&#x27;</span>:About&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//在实例里拿到路由表之后 我们就需要处理一下路由表的格式变成我们上面的那样 然后我们在定义一个默认的显示的路径 让我们的router-view去默认显示这个路径下的路由 </span></span><br><span class="line"><span class="comment">/* eslint-disable no-return-assign */</span></span><br><span class="line"><span class="keyword">let</span> Vue</span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Router</span> </span>&#123;</span><br><span class="line">  <span class="title">constructor</span> (<span class="params">&#123; routes &#125;</span>) &#123;</span><br><span class="line">    <span class="keyword">const</span> obj = &#123;&#125;</span><br><span class="line">    routes.forEach(<span class="function"><span class="params">item</span> =&gt;</span> &#123;</span><br><span class="line">      obj[item.path] = item.component</span><br><span class="line">    &#125;)</span><br><span class="line">    <span class="comment">// 这里建立映射关系 把我们的路由表遍历成了 &#123;&#x27;/&#x27;:Home,&#x27;/about&#x27;:About&#125;</span></span><br><span class="line">    <span class="built_in">this</span>.routeMap = obj</span><br><span class="line">    <span class="comment">// 定义了当前路由 也就是默认路由 然后我们根据路由表的映射关系去把组件渲染到我们的router-view里面去</span></span><br><span class="line">    <span class="built_in">this</span>.route = &#123; <span class="attr">current</span>: <span class="string">&#x27;/&#x27;</span> &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// 默认我们的Vue.use会调用函数install</span></span><br><span class="line"><span class="comment">// 所以我们需要在我们传进去的Router里写个install方法</span></span><br><span class="line"><span class="comment">// 这里的形参就是我们的Vue实例</span></span><br><span class="line"></span><br><span class="line">Router.install = <span class="function">(<span class="params">_Vue</span>) =&gt;</span> &#123;</span><br><span class="line">  Vue = _Vue</span><br><span class="line">  <span class="comment">// 为了在我们每个Vue的组件中拿到router实例 所以使用mixin方法 而不是使用原型上的那种添加方法</span></span><br><span class="line">  Vue.mixin(&#123;</span><br><span class="line">    <span class="comment">// 在每个组件的生命周期 之前调用 每个组件都会调用所以会一直执行类似递归</span></span><br><span class="line">    beforeCreate () &#123;</span><br><span class="line">      <span class="comment">// 判断你是不是最开始的根组件 也就是我们new Vue的时候传进去的配置</span></span><br><span class="line">      <span class="keyword">if</span> (<span class="built_in">this</span>.$options &amp;&amp; <span class="built_in">this</span>.$options.router) &#123;</span><br><span class="line">        <span class="comment">// 如果是我就赋值</span></span><br><span class="line">        <span class="built_in">this</span>._router = <span class="built_in">this</span>.$options.router</span><br><span class="line">      &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">        <span class="comment">// 如果不是我就继续向上找 这里你也可能没有父亲 可能是新new Vue 所以多了一层判断</span></span><br><span class="line">        <span class="built_in">this</span>._router = <span class="built_in">this</span>.$parent &amp;&amp; <span class="built_in">this</span>.$parent.router</span><br><span class="line">      &#125;</span><br><span class="line">      <span class="comment">// 以上就是让所有的组件都有了router的实例 也可拿到我们传进router构造函数的值</span></span><br><span class="line">      <span class="comment">// 为每个组件添加$router 和 $route 属性 这里的this就是当前的Vue实例也就是组件</span></span><br><span class="line">      <span class="built_in">Object</span>.defineProperty(<span class="built_in">this</span>, <span class="string">&#x27;$router&#x27;</span>, &#123;</span><br><span class="line">        value: &#123;&#125;</span><br><span class="line">      &#125;)</span><br><span class="line">      <span class="built_in">Object</span>.defineProperty(<span class="built_in">this</span>, <span class="string">&#x27;$route&#x27;</span>, &#123;</span><br><span class="line">        value: &#123;&#125;</span><br><span class="line">      &#125;)</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;)</span><br><span class="line">  <span class="comment">// 生成一个router-link组件 其实就是一个a标签 这里使用了jsx语法 能拿到你标签上传过来的to的值</span></span><br><span class="line">  Vue.component(<span class="string">&#x27;router-link&#x27;</span>, &#123;</span><br><span class="line">    props: &#123;</span><br><span class="line">      to: <span class="built_in">String</span></span><br><span class="line">    &#125;,</span><br><span class="line">    render () &#123;</span><br><span class="line">      <span class="keyword">return</span> <span class="xml"><span class="tag">&lt;<span class="name">a</span> <span class="attr">href</span>=<span class="string">&#123;</span>`#$&#123;<span class="attr">this.to</span>&#125;`&#125;&gt;</span>&#123;this.$slots.default&#125;<span class="tag">&lt;/<span class="name">a</span>&gt;</span></span></span><br><span class="line">    &#125;</span><br><span class="line">  &#125;)</span><br><span class="line">  <span class="comment">// 生成视口组件</span></span><br><span class="line">  Vue.component(<span class="string">&#x27;router-view&#x27;</span>, &#123;</span><br><span class="line">    props: &#123;</span><br><span class="line">      to: <span class="built_in">String</span></span><br><span class="line">    &#125;,</span><br><span class="line">    <span class="comment">//这里this是我们当前的组件 因为在上面的时候 也就是beforeCreate 我们已经把router的构造函数挂载到了我们的当前组件上 所以这里就能拿到我们定义的路由伊用的映射关系</span></span><br><span class="line">    render (h) &#123;</span><br><span class="line">      <span class="comment">//还记得我们便利的路由表的结构么  &#123;&#x27;/&#x27;:Home,&#x27;/about&#x27;:About&#125; 这样的 然后我们把当前的路由就切换过去了 </span></span><br><span class="line">      <span class="keyword">return</span> h(<span class="built_in">this</span>._router.routeMap[<span class="built_in">this</span>._router.route.current])</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;)</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> Router</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<h2 id="点击跳转路由-router-view切换不同的组件"><a href="#点击跳转路由-router-view切换不同的组件" class="headerlink" title="点击跳转路由 router-view切换不同的组件"></a>点击跳转路由 router-view切换不同的组件</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>上面我们已经设置当前路由 所以当我们点击按钮的时候 就应该把当前路由的路径改变 </span><br><span class="line"><span class="number">2.</span>然后去映射表里找到对应的路径的组件</span><br><span class="line"><span class="number">3.</span>首先我们在初次加载的时候要给显示默认的组件 </span><br><span class="line"><span class="number">4.</span>当我们点击按钮的时候 我们知道浏览器的地址栏会变成我们点击的那个路径 然后我们取到值 赋值给当前路由 然后我们的组件也会跟着改变</span><br></pre></td></tr></table></figure>
<figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/* eslint-disable no-unused-expressions */</span></span><br><span class="line"><span class="comment">/* eslint-disable no-return-assign */</span></span><br><span class="line"><span class="keyword">let</span> Vue</span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Router</span> </span>&#123;</span><br><span class="line">  <span class="title">constructor</span> (<span class="params">&#123; routes &#125;</span>) &#123;</span><br><span class="line">    <span class="keyword">const</span> obj = &#123;&#125;</span><br><span class="line">    routes.forEach(<span class="function"><span class="params">item</span> =&gt;</span> &#123;</span><br><span class="line">      obj[item.path] = item.component</span><br><span class="line">    &#125;)</span><br><span class="line">    <span class="built_in">this</span>.routeMap = obj</span><br><span class="line">    <span class="built_in">this</span>.route = &#123; <span class="attr">current</span>: <span class="string">&#x27;/&#x27;</span> &#125;</span><br><span class="line">    <span class="comment">// 初次加载的时候会监听laod事件 默认跳到/首页</span></span><br><span class="line">    <span class="built_in">window</span>.addEventListener(<span class="string">&#x27;load&#x27;</span>, <span class="function">() =&gt;</span> &#123;</span><br><span class="line">      location.hash ? <span class="string">&#x27;&#x27;</span> : location.hash = <span class="string">&#x27;/&#x27;</span> <span class="comment">// 默认就跳转到 首页</span></span><br><span class="line">    &#125;)</span><br><span class="line">    <span class="comment">// 当我们点击跳转的时候浏览器的地址栏会发生改变 用hashchange就可以监听到 然后我们取值在把路径赋值给我们的当前路由即可</span></span><br><span class="line">    <span class="built_in">window</span>.addEventListener(<span class="string">&#x27;hashchange&#x27;</span>, <span class="function">() =&gt;</span> &#123;</span><br><span class="line">      <span class="built_in">this</span>.route.current = location.hash.slice(<span class="number">1</span>)</span><br><span class="line">    &#125;)</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line">Router.install = <span class="function">(<span class="params">_Vue</span>) =&gt;</span> &#123;</span><br><span class="line">  Vue = _Vue</span><br><span class="line">  Vue.mixin(&#123;</span><br><span class="line">    beforeCreate () &#123;</span><br><span class="line">      <span class="keyword">if</span> (<span class="built_in">this</span>.$options &amp;&amp; <span class="built_in">this</span>.$options.router) &#123;</span><br><span class="line">        <span class="built_in">this</span>._router = <span class="built_in">this</span>.$options.router</span><br><span class="line">      &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">        <span class="built_in">this</span>._router = <span class="built_in">this</span>.$parent &amp;&amp; <span class="built_in">this</span>.$parent.router</span><br><span class="line">      &#125;</span><br><span class="line">      <span class="built_in">Object</span>.defineProperty(<span class="built_in">this</span>, <span class="string">&#x27;$router&#x27;</span>, &#123;</span><br><span class="line">        value: &#123;&#125;</span><br><span class="line">      &#125;)</span><br><span class="line">      <span class="built_in">Object</span>.defineProperty(<span class="built_in">this</span>, <span class="string">&#x27;$route&#x27;</span>, &#123;</span><br><span class="line">        value: &#123;&#125;</span><br><span class="line">      &#125;)</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;)</span><br><span class="line">  Vue.component(<span class="string">&#x27;router-link&#x27;</span>, &#123;</span><br><span class="line">    props: &#123;</span><br><span class="line">      to: <span class="built_in">String</span></span><br><span class="line">    &#125;,</span><br><span class="line">    render () &#123;</span><br><span class="line">      <span class="keyword">return</span> <span class="xml"><span class="tag">&lt;<span class="name">a</span> <span class="attr">href</span>=<span class="string">&#123;</span>`#$&#123;<span class="attr">this.to</span>&#125;`&#125;&gt;</span>&#123;this.$slots.default&#125;<span class="tag">&lt;/<span class="name">a</span>&gt;</span></span></span><br><span class="line">    &#125;</span><br><span class="line">  &#125;)</span><br><span class="line">  Vue.component(<span class="string">&#x27;router-view&#x27;</span>, &#123;</span><br><span class="line">    props: &#123;</span><br><span class="line">      to: <span class="built_in">String</span></span><br><span class="line">    &#125;,</span><br><span class="line">    render (h) &#123;</span><br><span class="line">      <span class="keyword">return</span> h(<span class="built_in">this</span>._router.routeMap[<span class="built_in">this</span>._router.route.current])</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;)</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> Router</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<p><img src="2.gif" alt=""></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//这里看到我们的地址栏是变的了 但是我们的组件并没有没渲染出来 这是因为我们的这里的赋值给当前路径不是响应式的 所以没有发生改变</span></span><br></pre></td></tr></table></figure>
<h2 id="当前路由路径响应式"><a href="#当前路由路径响应式" class="headerlink" title="当前路由路径响应式"></a>当前路由路径响应式</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>因为数据现在不是响应式的 所以没有发生改变</span><br><span class="line"><span class="number">2.</span>这里我们使用Vue自带的工具 把我们的<span class="built_in">this</span>.current 变成响应式即可</span><br></pre></td></tr></table></figure>
<figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/* eslint-disable no-unused-expressions */</span></span><br><span class="line"><span class="comment">/* eslint-disable no-return-assign */</span></span><br><span class="line"><span class="keyword">let</span> Vue</span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Router</span> </span>&#123;</span><br><span class="line">  <span class="title">constructor</span> (<span class="params">&#123; routes &#125;</span>) &#123;</span><br><span class="line">    <span class="keyword">const</span> obj = &#123;&#125;</span><br><span class="line">    routes.forEach(<span class="function"><span class="params">item</span> =&gt;</span> &#123;</span><br><span class="line">      obj[item.path] = item.component</span><br><span class="line">    &#125;)</span><br><span class="line">    <span class="comment">// 这里建立映射关系 把我们的路由表遍历成了 &#123;&#x27;/&#x27;:Home,&#x27;/about&#x27;:About&#125;</span></span><br><span class="line">    <span class="built_in">this</span>.routeMap = obj</span><br><span class="line">    <span class="comment">// 定义了当前路由 也就是点击的那个 变成响应式数据</span></span><br><span class="line">    Vue.util.defineReactive(<span class="built_in">this</span>, <span class="string">&#x27;route&#x27;</span>, &#123; <span class="attr">current</span>: <span class="string">&#x27;/&#x27;</span> &#125;)</span><br><span class="line">    <span class="built_in">window</span>.addEventListener(<span class="string">&#x27;load&#x27;</span>, <span class="function">() =&gt;</span> &#123;</span><br><span class="line">      location.hash ? <span class="string">&#x27;&#x27;</span> : location.hash = <span class="string">&#x27;/&#x27;</span> <span class="comment">// 默认就跳转到 首页</span></span><br><span class="line">    &#125;)</span><br><span class="line">    <span class="built_in">window</span>.addEventListener(<span class="string">&#x27;hashchange&#x27;</span>, <span class="function">() =&gt;</span> &#123;</span><br><span class="line">      <span class="comment">//当路由跳转变化的时候 监测到 然后我们就把浏览器的地址栏的路径取到 赋值给我们的当前路径  </span></span><br><span class="line">      <span class="built_in">this</span>.route.current = location.hash.slice(<span class="number">1</span>)</span><br><span class="line">    &#125;)</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line">Router.install = <span class="function">(<span class="params">_Vue</span>) =&gt;</span> &#123;</span><br><span class="line">  Vue = _Vue</span><br><span class="line">  Vue.mixin(&#123;</span><br><span class="line">    beforeCreate () &#123;</span><br><span class="line">      <span class="keyword">if</span> (<span class="built_in">this</span>.$options &amp;&amp; <span class="built_in">this</span>.$options.router) &#123;</span><br><span class="line">        <span class="built_in">this</span>._router = <span class="built_in">this</span>.$options.router</span><br><span class="line">      &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">        <span class="built_in">this</span>._router = <span class="built_in">this</span>.$parent &amp;&amp; <span class="built_in">this</span>.$parent.router</span><br><span class="line">      &#125;</span><br><span class="line">      <span class="built_in">Object</span>.defineProperty(<span class="built_in">this</span>, <span class="string">&#x27;$router&#x27;</span>, &#123;</span><br><span class="line">        value: &#123;&#125;</span><br><span class="line">      &#125;)</span><br><span class="line">      <span class="built_in">Object</span>.defineProperty(<span class="built_in">this</span>, <span class="string">&#x27;$route&#x27;</span>, &#123;</span><br><span class="line">        value: &#123;&#125;</span><br><span class="line">      &#125;)</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;)</span><br><span class="line">  Vue.component(<span class="string">&#x27;router-link&#x27;</span>, &#123;</span><br><span class="line">    props: &#123;</span><br><span class="line">      to: <span class="built_in">String</span></span><br><span class="line">    &#125;,</span><br><span class="line">    render () &#123;</span><br><span class="line">      <span class="keyword">return</span> <span class="xml"><span class="tag">&lt;<span class="name">a</span> <span class="attr">href</span>=<span class="string">&#123;</span>`#$&#123;<span class="attr">this.to</span>&#125;`&#125;&gt;</span>&#123;this.$slots.default&#125;<span class="tag">&lt;/<span class="name">a</span>&gt;</span></span></span><br><span class="line">    &#125;</span><br><span class="line">  &#125;)</span><br><span class="line">  Vue.component(<span class="string">&#x27;router-view&#x27;</span>, &#123;</span><br><span class="line">    props: &#123;</span><br><span class="line">      to: <span class="built_in">String</span></span><br><span class="line">    &#125;,</span><br><span class="line">    render (h) &#123;</span><br><span class="line">      <span class="comment">//这里由于浏览器的路径变化了 所有我们的对应的组件也变化了  </span></span><br><span class="line">      <span class="keyword">return</span> h(<span class="built_in">this</span>._router.routeMap[<span class="built_in">this</span>._router.route.current])</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;)</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> Router</span><br></pre></td></tr></table></figure>
<p><img src="3.gif" alt=""></p>
<h2 id="总结"><a href="#总结" class="headerlink" title="总结"></a>总结</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>Vue.use的时候 我们会默认调用install这个函数 形参是Vue实例</span><br><span class="line"><span class="number">2.</span>为了我们能在每个组件里把router的实例挂在到组件上 所以我们使用了mixin 的混合 而没有使用Vue.prototype.router</span><br><span class="line"><span class="number">3.</span>然后我们创建了两个组件router-link 和 router-view 一个渲染成a标签 在点击的时候会跳转 然后浏览器的路径也会变</span><br><span class="line"><span class="number">4.</span>然后我们可以在router的实例中拿到你传进来的配置项 把路由表变成 路径:组件 这样的对象 建立映射关系</span><br><span class="line"><span class="number">5.</span>然后定义当前路径 这里用了Vue.util 把这个当前路径变成响应式</span><br><span class="line"><span class="number">6.</span>初次加载监听了浏览器的load事件 并把当前路由设置了一下</span><br><span class="line"><span class="number">7.</span>当点击router-link的时候 浏览器的地址会跳转 hash也会发生改变 这个时候我们又监听了 并且把浏览器跳转的路径地址做了截取出来</span><br><span class="line"><span class="number">8.</span>然后router-view 根据我们建立的映射关系表 渲染不同的组件</span><br><span class="line"><span class="number">9.</span>代码 地址 https:<span class="comment">//github.com/wazer1987/mini_VueRouter</span></span><br></pre></td></tr></table></figure>
</article><div class="post-copyright"><div class="post-copyright__title"><span class="post-copyright-info"><h>手写一个mini版本vue-router</h></span></div><div class="post-copyright__type"><span class="post-copyright-info"><a href="http://example.com/2019/03/18/%E6%89%8B%E5%86%99%E4%B8%80%E4%B8%AAmini%E7%89%88vue-router/">http://example.com/2019/03/18/%E6%89%8B%E5%86%99%E4%B8%80%E4%B8%AAmini%E7%89%88vue-router/</a></span></div><div class="post-copyright-m"><div class="post-copyright-m-info"><div class="post-copyright-a"><h>作者</h><div class="post-copyright-cc-info"><h>北城韩雨</h></div></div><div class="post-copyright-c"><h>发布于</h><div class="post-copyright-cc-info"><h>2019-03-18</h></div></div><div class="post-copyright-u"><h>更新于</h><div class="post-copyright-cc-info"><h>2019-06-03</h></div></div><div class="post-copyright-c"><h>许可协议</h><div class="post-copyright-cc-info"><a class="icon" rel="noopener" target="_blank" title="Creative Commons" href="https://creativecommons.org/"><i class="fab fa-creative-commons"></i></a><a rel="noopener" target="_blank" title="CC BY-NC-SA 4.0" href="https://creativecommons.org/licenses/by-nc-sa/4.0/">CC BY-NC-SA 4.0</a></div></div></div></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/Vue-Router/">Vue-Router</a></div><div class="post_share"></div></div><link rel="stylesheet" href="/" media="defer" onload="this.media='all'"/><div class="post-reward"><button class="tip-button reward-button"><span class="tip-button__text">打赏</span><div class="coin-wrapper"><div class="coin"><div class="coin__middle"></div><div class="coin__back"></div><div class="coin__front"></div></div></div><div class="reward-main"><ul class="reward-all"><li class="reward-item"><a href="/img/weixin.jpg" target="_blank"><img class="post-qr-code-img" src="/img/weixin.jpg" alt="微信"/></a><div class="post-qr-code-desc">微信</div></li><li class="reward-item"><a href="/img/taobao.jpg" target="_blank"><img class="post-qr-code-img" src="/img/taobao.jpg" alt="支付宝"/></a><div class="post-qr-code-desc">支付宝</div></li></ul></div></button></div><script defer="defer" src="/"></script><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/2019/03/19/%E6%89%8B%E5%86%99%E4%B8%80%E4%B8%AAmini%E7%89%88%E7%9A%84Vuex/"><img class="prev-cover" src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic.vjshi.com%2F2018-07-17%2F3eca5394963b50e37b7be2ba320029ec%2F00003.jpg%3Fx-oss-process%3Dstyle%2Fwatermark&amp;refer=http%3A%2F%2Fpic.vjshi.com&amp;app=2002&amp;size=f9999,10000&amp;q=a80&amp;n=0&amp;g=0n&amp;fmt=jpeg?sec=1629530644&amp;t=10679b5732c52e39eeb588040240a790" onerror="onerror=null;src='/img/404.jpg'" alt="cover of previous post"><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">手写一个mini版Vuex</div></div></a></div><div class="next-post pull-right"><a href="/2019/03/16/%E6%89%8B%E5%86%99%E4%B8%80%E4%B8%AAlow%E7%A2%A7%E7%89%88Vue/"><img class="next-cover" src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic.vjshi.com%2F2017-07-20%2F4f06f9aa2ebcea6691a78537052e8e9f%2F00001.jpg%3Fx-oss-process%3Dstyle%2Fwatermark&amp;refer=http%3A%2F%2Fpic.vjshi.com&amp;app=2002&amp;size=f9999,10000&amp;q=a80&amp;n=0&amp;g=0n&amp;fmt=jpeg?sec=1629530599&amp;t=32fb97bad7007bdc6c712ccadacd5721" onerror="onerror=null;src='/img/404.jpg'" alt="cover of next post"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">手写一个low碧版Vue</div></div></a></div></nav></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src="/img/my.jpg" onerror="this.onerror=null;this.src='/img/friend_404.gif'" alt="avatar"/></div><div class="author-info__name">北城韩雨</div><div class="author-info__description">一个前端界的小学生</div></div><div class="card-info-data"><div class="card-info-data-item is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">65</div></a></div><div class="card-info-data-item is-center"><a href="/tags/"><div class="headline">标签</div><div class="length-num">23</div></a></div><div class="card-info-data-item is-center"><a href="/categories/"><div class="headline">分类</div><div class="length-num">21</div></a></div></div><a class="button--animated" id="card-info-btn" target="_blank" rel="noopener" href="https://github.com/wazer1987"><i class="fab fa-github"></i><span>Follow Me</span></a><div class="card-info-social-icons is-center"><a class="social-icon" href="/wangzhengvslike@163.com" target="_blank" title="Email"><i class="fas fa-envelope"></i></a></div></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn card-announcement-animation"></i><span>公告</span></div><div class="announcement_content"><p>感谢访问本站，如喜欢请收藏。本站主要分享前端知识，立志成为资深前端工程师，但目前是一个前端界的小学生 若喜欢可以打赏请博主喝一杯冰阔落</p> <p>另外请大家多多支持淼哥的开源项目</p> <p>https://github.com/flipped-aurora/gin-vue-admin</p> <p>🌟🌟欢迎大家start 🌟🌟</p> <p>欢迎加入博主的前端技术交流群</p> <p style="background:rgba(255,255,255,1);margin:0"> <img src="/img/code.png"></img> </p></div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#mini%E7%89%88%E6%9C%ACvue-router"><span class="toc-text">mini版本vue-router</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#router%E7%9A%84%E6%8C%82%E8%BD%BD%E6%B5%81%E7%A8%8B"><span class="toc-text">router的挂载流程</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Vue-use%E9%83%BD%E5%B9%B2%E4%BA%86%E4%BB%80%E4%B9%88"><span class="toc-text">Vue.use都干了什么</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%A6%82%E4%BD%95%E5%9C%A8%E6%AF%8F%E4%B8%AA%E7%BB%84%E4%BB%B6%E4%B8%8A%E6%8B%BF%E5%88%B0router%E5%AE%9E%E4%BE%8B"><span class="toc-text">如何在每个组件上拿到router实例</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Vue-mixin-API"><span class="toc-text">Vue.mixin() API</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E7%BB%99%E6%AF%8F%E4%B8%AA%E7%BB%84%E4%BB%B6%E9%83%BD%E6%B7%BB%E5%8A%A0router%E5%AE%9E%E4%BE%8B"><span class="toc-text">给每个组件都添加router实例</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%B7%BB%E5%8A%A0%E5%B1%9E%E6%80%A7"><span class="toc-text">添加属性</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E7%94%9F%E6%88%90%E7%BB%84%E4%BB%B6"><span class="toc-text">生成组件</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%8B%BF%E5%88%B0%E8%B7%AF%E7%94%B1%E8%A1%A8"><span class="toc-text">拿到路由表</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E7%82%B9%E5%87%BB%E8%B7%B3%E8%BD%AC%E8%B7%AF%E7%94%B1-router-view%E5%88%87%E6%8D%A2%E4%B8%8D%E5%90%8C%E7%9A%84%E7%BB%84%E4%BB%B6"><span class="toc-text">点击跳转路由 router-view切换不同的组件</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%BD%93%E5%89%8D%E8%B7%AF%E7%94%B1%E8%B7%AF%E5%BE%84%E5%93%8D%E5%BA%94%E5%BC%8F"><span class="toc-text">当前路由路径响应式</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%80%BB%E7%BB%93"><span class="toc-text">总结</span></a></li></ol></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item"><a class="thumbnail" href="/2021/04/16/gin-vue-admin/" title="Gin-Vue-Admin"><img src="/img/gin-vue-admin.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="Gin-Vue-Admin"/></a><div class="content"><a class="title" href="/2021/04/16/gin-vue-admin/" title="Gin-Vue-Admin">Gin-Vue-Admin</a><time datetime="2021-04-15T16:00:00.000Z" title="发表于 2021-04-16 00:00:00">2021-04-16</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2021/01/08/Vue%E6%BA%90%E7%A0%81%E5%BC%80%E5%8F%91%E4%B9%8B%E7%BB%84%E4%BB%B6%E7%9A%84%E6%B8%B2%E6%9F%9310/" title="Vue源码开发-组件的渲染10"><img src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic.vjshi.com%2F2018-07-17%2F3eca5394963b50e37b7be2ba320029ec%2F00003.jpg%3Fx-oss-process%3Dstyle%2Fwatermark&amp;refer=http%3A%2F%2Fpic.vjshi.com&amp;app=2002&amp;size=f9999,10000&amp;q=a80&amp;n=0&amp;g=0n&amp;fmt=jpeg?sec=1629530644&amp;t=10679b5732c52e39eeb588040240a790" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="Vue源码开发-组件的渲染10"/></a><div class="content"><a class="title" href="/2021/01/08/Vue%E6%BA%90%E7%A0%81%E5%BC%80%E5%8F%91%E4%B9%8B%E7%BB%84%E4%BB%B6%E7%9A%84%E6%B8%B2%E6%9F%9310/" title="Vue源码开发-组件的渲染10">Vue源码开发-组件的渲染10</a><time datetime="2021-01-07T16:00:00.000Z" title="发表于 2021-01-08 00:00:00">2021-01-08</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2021/01/07/Vue%E6%BA%90%E7%A0%81%E5%BC%80%E5%8F%91%E4%B9%8B%E7%BB%84%E4%BB%B6%E7%9A%84%E5%90%88%E5%B9%B6%E7%AD%96%E7%95%A509/" title="Vue源码开发-组件合并策略09"><img src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fcz-video-photo.oss-cn-beijing.aliyuncs.com%2F20191017%2F4cfc341591d242327b67e9ceb6953612_00002.jpg&amp;refer=http%3A%2F%2Fcz-video-photo.oss-cn-beijing.aliyuncs.com&amp;app=2002&amp;size=f9999,10000&amp;q=a80&amp;n=0&amp;g=0n&amp;fmt=jpeg?sec=1629530702&amp;t=80943e12d5239f63beafd6d1c540855f" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="Vue源码开发-组件合并策略09"/></a><div class="content"><a class="title" href="/2021/01/07/Vue%E6%BA%90%E7%A0%81%E5%BC%80%E5%8F%91%E4%B9%8B%E7%BB%84%E4%BB%B6%E7%9A%84%E5%90%88%E5%B9%B6%E7%AD%96%E7%95%A509/" title="Vue源码开发-组件合并策略09">Vue源码开发-组件合并策略09</a><time datetime="2021-01-06T16:00:00.000Z" title="发表于 2021-01-07 00:00:00">2021-01-07</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2021/01/06/Vue%E6%BA%90%E7%A0%81%E5%BC%80%E5%8F%91%E4%B9%8B%E6%95%B0%E7%BB%84%E7%9A%84%E4%BE%9D%E8%B5%96%E6%94%B6%E9%9B%8608/" title="Vue源码开发-数组的依赖收集08"><img src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic.vjshi.com%2F2018-07-17%2F3eca5394963b50e37b7be2ba320029ec%2F00003.jpg%3Fx-oss-process%3Dstyle%2Fwatermark&amp;refer=http%3A%2F%2Fpic.vjshi.com&amp;app=2002&amp;size=f9999,10000&amp;q=a80&amp;n=0&amp;g=0n&amp;fmt=jpeg?sec=1629530644&amp;t=10679b5732c52e39eeb588040240a790" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="Vue源码开发-数组的依赖收集08"/></a><div class="content"><a class="title" href="/2021/01/06/Vue%E6%BA%90%E7%A0%81%E5%BC%80%E5%8F%91%E4%B9%8B%E6%95%B0%E7%BB%84%E7%9A%84%E4%BE%9D%E8%B5%96%E6%94%B6%E9%9B%8608/" title="Vue源码开发-数组的依赖收集08">Vue源码开发-数组的依赖收集08</a><time datetime="2021-01-05T16:00:00.000Z" title="发表于 2021-01-06 00:00:00">2021-01-06</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2021/01/06/Vue%E6%BA%90%E7%A0%81%E5%BC%80%E5%8F%91%E4%B9%8B%E7%94%9F%E5%91%BD%E5%91%A8%E6%9C%9F%E5%90%88%E5%B9%B607/" title="Vue源码开发-生命周期合并07"><img src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic.vjshi.com%2F2018-07-17%2Faf41dabad727ba80c9ca8a2bfbd22a58%2F00004.jpg%3Fx-oss-process%3Dstyle%2Fwatermark&amp;refer=http%3A%2F%2Fpic.vjshi.com&amp;app=2002&amp;size=f9999,10000&amp;q=a80&amp;n=0&amp;g=0n&amp;fmt=jpeg?sec=1629530659&amp;t=bbcfe1e86f393eecbb0722db1b92e475" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="Vue源码开发-生命周期合并07"/></a><div class="content"><a class="title" href="/2021/01/06/Vue%E6%BA%90%E7%A0%81%E5%BC%80%E5%8F%91%E4%B9%8B%E7%94%9F%E5%91%BD%E5%91%A8%E6%9C%9F%E5%90%88%E5%B9%B607/" title="Vue源码开发-生命周期合并07">Vue源码开发-生命周期合并07</a><time datetime="2021-01-05T16:00:00.000Z" title="发表于 2021-01-06 00:00:00">2021-01-06</time></div></div></div></div></div></div></main><footer id="footer"><div id="footer-wrap"><div class="copyright"><span>&copy;2017 - 2021</span><svg style="width:2em; height:2em;position: relative;top: 10px;" aria-hidden="true"><use xlink:href="#icon-Butterfly"></use></svg><span>北城韩雨</span></div><div class="framework-info"><span>框架 </span><a target="_blank" rel="noopener" href="https://hexo.io">Hexo</a><span class="footer-separator">|</span><span>主题 </span><a target="_blank" rel="noopener" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a></div><div class="footer_custom_text"><p><a style="margin-inline:5px" target="_blank" href="https://hexo.io/"><img src="https://img.shields.io/badge/Frame-Hexo-blue?style=flat&logo=hexo" title="博客框架为Hexo"></a><a style="margin-inline:5px" target="_blank" href="https://butterfly.js.org/"><img src="https://img.shields.io/badge/Theme-Butterfly-6513df?style=flat&logo=bitdefender" title="主题采用butterfly"></a><a style="margin-inline:5px" target="_blank" href="https://www.jsdelivr.com/"><img src="https://img.shields.io/badge/CDN-jsDelivr-orange?style=flat&logo=jsDelivr" title="本站使用JsDelivr为静态资源提供CDN加速"></a></p></div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><button id="go-up" type="button" title="回到顶部"><i class="fas fa-arrow-up"></i></button></div></div><div><script src="/js/utils.js"></script><script src="/js/main.js"></script><script src="https://cdn.jsdelivr.net/npm/medium-zoom/dist/medium-zoom.min.js"></script><div class="js-pjax"></div><script defer src="/live2d-widget/autoload.js"></script><script src="/js/coin.js"></script><script async src="//at.alicdn.com/t/font_2700632_3rtvhzzpl3h.js"></script><script src="/js/liuxing.js" data-pjax></script><canvas class="fireworks" mobile="false"></canvas><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/fireworks.min.js"></script><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/activate-power-mode.min.js"></script><script>POWERMODE.colorful = true;
POWERMODE.shake = true;
POWERMODE.mobile = false;
document.body.addEventListener('input', POWERMODE);
</script><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div><!-- hexo injector body_end start --><div class="js-pjax"><script async="async">var arr = document.getElementsByClassName('recent-post-item');
for(var i = 0;i<arr.length;i++){
    arr[i].classList.add('wow');
    arr[i].classList.add('animate__zoomIn');
    arr[i].setAttribute('data-wow-duration', '1s');
    arr[i].setAttribute('data-wow-delay', '0ms');
    arr[i].setAttribute('data-wow-offset', '100');
    arr[i].setAttribute('data-wow-iteration', '1');
  }</script><script async="async">var arr = document.getElementsByClassName('card-widget');
for(var i = 0;i<arr.length;i++){
    arr[i].classList.add('wow');
    arr[i].classList.add('animate__zoomIn');
    arr[i].setAttribute('data-wow-duration', '');
    arr[i].setAttribute('data-wow-delay', '');
    arr[i].setAttribute('data-wow-offset', '');
    arr[i].setAttribute('data-wow-iteration', '');
  }</script></div><script defer src="https://cdn.jsdelivr.net/gh/graingert/wow@1.3.0/dist/wow.min.js"></script><script defer src="https://cdn.jsdelivr.net/npm/hexo-butterfly-wowjs/lib/wow_init.min.js"></script><script async src="//at.alicdn.com/t/font_2032782_8d5kxvn09md.js"></script><!-- hexo injector body_end end --></body></html>